package edu.utility.Gui;

import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import edu.utility.Entities.Entities;
import edu.utility.Entities.User;

public class SettingPane extends JPanel{
	
	private JCheckBox streamMovies, streamAudio, streamPictures;
	private JButton logout, save, start;
	private JSlider slider;
	private ImagePanel image;
	private MyFrame frame;
	private JTextField portField;
	
	public SettingPane(MyFrame frame){
		this.frame = frame;
		this.setLayout(null);
		this.add(new VideoPanel());
		this.add(new StreamingPanel());
		this.setBounds(0, 0, frame.getWidth(), frame.getHeight());
		addButtons();
		setUpPort();
	}
	private void addButtons(){
		logout = new JButton("logout");
		logout.setBounds(130, 272, 125, 25);
		logout.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				Entities.network.setUser(null);
				frame.removeControls();
				frame.showLogIn();
			}
			
		});
		this.add(logout);
		
		save = new JButton("save");
		save.setBounds(130, 237, 125, 25);
		save.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				Entities.network.getUser().setStreamAudio(streamAudio.isSelected()?1:0);
				Entities.network.getUser().setStreamMovies(streamMovies.isSelected()?1:0);
				Entities.network.getUser().setStreamPictures(streamPictures.isSelected()?1:0);
				Entities.network.getUser().setVideoQuality(slider.getValue());
				Entities.network.getUser().setPort(Integer.parseInt(portField.getText()));
				Entities.network.updateUser();
			}
		});
		this.add(save);
	}
	public void setUpPort(){
		JLabel portLabel = new JLabel("Port:");
		portLabel.setBounds(130, 210, 55, 20);
		this.add(portLabel);
		
		
		portField = new JTextField();
		portField.setText("" + Entities.network.getUser().getPort());
		portField.setBounds(170,  210, 70, 20);
		this.add(portField);
	}
	
	
	class VideoPanel extends JPanel{
		VideoPanel(){
			this.setBounds(7, 5, 270, 200);
			this.setLayout(null);
			this.setBorder(BorderFactory.createTitledBorder("Video Settings"));
			JLabel localLabel = new JLabel("Video Quality");
			localLabel.setBounds(90, 10, 74, 25);
			this.add(localLabel);
			
			slider = new JSlider(JSlider.HORIZONTAL, 1, 10, 10);
			slider.setMajorTickSpacing(1);
			slider.setPaintTicks(true);
			slider.setBounds(10, 35, 250, 30);
			slider.setValue(Entities.network.getUser().getVideoQuality());
			slider.addChangeListener(new ChangeListener(){
				public void stateChanged(ChangeEvent arg0) {
					changeImageQuality();
				}
				
			});
			this.add(slider);
			
			image = new ImagePanel();
			
			this.add(image);
			changeImageQuality();
		}
		
		private void changeImageQuality(){
			try{
			image.changeQuality(slider.getValue());
			}catch(Exception ex){}
		}
	}
	class StreamingPanel extends JPanel{
		StreamingPanel(){
			this.setBounds(7, 205, 100, 100);
			this.setLayout(new BoxLayout(this, 1));
			this.setBorder(BorderFactory.createTitledBorder("Streaming"));
			
			streamMovies = new JCheckBox  ("Movies"); this.add(streamMovies);
			streamMovies.setSelected(Entities.network.getUser().isStreamMovies());
			
			streamAudio= new JCheckBox    ("Audio"); this.add(streamAudio);
			streamAudio.setSelected(Entities.network.getUser().isStreamAudio());
			
			streamPictures = new JCheckBox("Pictures");this.add(streamPictures);
			streamPictures.setSelected(Entities.network.getUser().isStreamPictures());
		}
	}

}
